 package handling.world;

 import client.MapleCharacter;
 import handling.channel.ChannelServer;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;









 public class WorldFindService
 {
/*  23 */   private static final Logger log = LogManager.getLogger(WorldFindService.class.getName());
   private final ReentrantReadWriteLock lock;
   private final HashMap<Integer, Integer> idToChannel;
   private final HashMap<Integer, Integer> accidToChannel;
   private final HashMap<String, Integer> nameToChannel;

   private WorldFindService() {
/*  30 */     log.info("正在启动[WorldFindService]");
/*  31 */     this.lock = new ReentrantReadWriteLock();
/*  32 */     this.idToChannel = new HashMap<>();
/*  33 */     this.accidToChannel = new HashMap<>();
/*  34 */     this.nameToChannel = new HashMap<>();
   }

   public static WorldFindService getInstance() {
/*  38 */     return SingletonHolder.instance;
   }

   public void register(int chrId, int accId, String chrName, int channel) {
/*  42 */     this.lock.writeLock().lock();
     try {
/*  44 */       this.idToChannel.put(Integer.valueOf(chrId), Integer.valueOf(channel));
/*  45 */       this.accidToChannel.put(Integer.valueOf(accId), Integer.valueOf(channel));
/*  46 */       this.nameToChannel.put(chrName.toLowerCase(), Integer.valueOf(channel));
     } finally {
/*  48 */       this.lock.writeLock().unlock();
     }
/*  50 */     if (channel == -10) {
/*  51 */       System.out.println("玩家连接 - 角色ID: " + chrId + " 名字: " + chrName + " 进入商城");
/*  52 */     } else if (channel == -20) {
/*  53 */       System.out.println("玩家连接 - 角色ID: " + chrId + " 名字: " + chrName + " 进入拍卖");
/*  54 */     } else if (channel > -1) {
/*  55 */       System.out.println("玩家连接 - 角色ID: " + chrId + " 名字: " + chrName + " 频道: " + channel);
     } else {
/*  57 */       System.out.println("玩家连接 - 角色ID: " + chrId + " 未处理的频道...");
     }
   }


   public void forceDeregister(int chrId) {
     try {
/*  64 */       this.idToChannel.remove(Integer.valueOf(chrId));
     } finally {
/*  66 */       System.out.println("玩家离开 - 出现错误: forceDeregister63");
     }

/*  69 */     System.out.println("玩家离开 - 角色ID: " + chrId);
   }

   public void forceDeregisterByAccId(int accId) {
/*  73 */     this.lock.writeLock().lock();
     try {
/*  75 */       this.accidToChannel.remove(Integer.valueOf(accId));
     } finally {
/*  77 */       this.lock.writeLock().unlock();
     }
/*  79 */     System.out.println("玩家离开 - 角色ID: " + accId);
   }


   public void forceDeregister(String chrName) {
     try {
/*  85 */       this.nameToChannel.remove(chrName.toLowerCase());
     } finally {
/*  87 */       System.out.println("玩家离开 - 出现错误: forceDeregister74");
     }
/*  89 */     System.out.println("玩家离开 - 角色名字: " + chrName);
   }

   public void forceDeregister(int chrId, String chrName, int accId) {
/*  93 */     this.lock.writeLock().lock();
     try {
/*  95 */       this.idToChannel.remove(Integer.valueOf(chrId));
/*  96 */       this.accidToChannel.remove(Integer.valueOf(accId));
/*  97 */       this.nameToChannel.remove(chrName.toLowerCase());
     } finally {
/*  99 */       this.lock.writeLock().unlock();
     }
/* 101 */     System.out.println("玩家离开 - 角色ID: " + chrId + " 名字: " + chrName);
/* 102 */     log.trace("玩家离开", new Throwable());
   }


   public void forceDeregister(int chrId, String chrName) {
     try {
/* 108 */       this.idToChannel.remove(Integer.valueOf(chrId));
/* 109 */       this.nameToChannel.remove(chrName.toLowerCase());
     } finally {
/* 111 */       System.out.println("玩家离开 - 出现错误: forceDeregister85");
     }
/* 113 */     System.out.println("玩家离开 - 角色ID: " + chrId + " 名字: " + chrName);
   }

   public void forceDeregisterEx(int chrId, String chrName) {
/* 117 */     this.lock.writeLock().lock();
     try {
/* 119 */       this.idToChannel.remove(Integer.valueOf(chrId));
/* 120 */       this.nameToChannel.remove(chrName.toLowerCase());
     } finally {
/* 122 */       this.lock.writeLock().unlock();
     }
/* 124 */     System.out.println("清理卡号玩家 - 角色ID: " + chrId + " 名字: " + chrName);
   }




   public int findChannel(int chrId) {
     Integer ret;
/* 132 */     this.lock.readLock().lock();
     try {
/* 134 */       ret = this.idToChannel.get(Integer.valueOf(chrId));
     } finally {
/* 136 */       this.lock.readLock().unlock();
     }
/* 138 */     if (ret != null) {
/* 139 */       if (ret.intValue() != -10 && ret.intValue() != -20 && ChannelServer.getInstance(ret.intValue()) == null) {
/* 140 */         forceDeregister(chrId);
/* 141 */         return -1;
       }
/* 143 */       return ret.intValue();
     }
/* 145 */     return -1;
   }




   public int findChannelByAccId(int accId) {
     Integer ret;
/* 153 */     this.lock.readLock().lock();
     try {
/* 155 */       ret = this.accidToChannel.get(Integer.valueOf(accId));
     } finally {
/* 157 */       this.lock.readLock().unlock();
     }
/* 159 */     if (ret != null) {
/* 160 */       if (ret.intValue() != -10 && ret.intValue() != -20 && ChannelServer.getInstance(ret.intValue()) == null) {
/* 161 */         forceDeregisterByAccId(accId);
/* 162 */         return -1;
       }
/* 164 */       return ret.intValue();
     }
/* 166 */     return -1;
   }




   public int findChannel(String chrName) {
     Integer ret;
/* 174 */     this.lock.readLock().lock();
     try {
/* 176 */       ret = this.nameToChannel.get(chrName.toLowerCase());
     } finally {
/* 178 */       this.lock.readLock().unlock();
     }
/* 180 */     if (ret != null) {



/* 184 */       if (ret.intValue() != -10 && ret.intValue() != -20 && ChannelServer.getInstance(ret.intValue()) == null) {
/* 185 */         forceDeregister(chrName);
/* 186 */         return -1;
       }
/* 188 */       return ret.intValue();
     }
/* 190 */     return -1;
   }




   public CharacterIdChannelPair[] multiBuddyFind(int charIdFrom, int[] characterIds) {
/* 197 */     List<CharacterIdChannelPair> foundsChars = new ArrayList<>(characterIds.length);
/* 198 */     for (int i : characterIds) {
/* 199 */       int channel = findChannel(i);
/* 200 */       if (channel > 0) {
/* 201 */         foundsChars.add(new CharacterIdChannelPair(i, channel));
       }
     }
/* 204 */     Collections.sort(foundsChars);
/* 205 */     return foundsChars.<CharacterIdChannelPair>toArray(new CharacterIdChannelPair[foundsChars.size()]);
   }




   public MapleCharacter findCharacterByAccId(int id) {
/* 212 */     int ch = findChannelByAccId(id);
/* 213 */     if (ch > 0) {
/* 214 */       return ChannelServer.getInstance(ch).getPlayerStorage().getCharacterById(id);
     }
/* 216 */     return null;
   }




   public MapleCharacter findCharacterByName(String name) {
/* 223 */     int ch = findChannel(name);
/* 224 */     if (ch > 0) {
/* 225 */       return ChannelServer.getInstance(ch).getPlayerStorage().getCharacterByName(name);
     }
/* 227 */     return null;
   }




   public MapleCharacter findCharacterById(int id) {
/* 234 */     int ch = findChannel(id);
/* 235 */     if (ch > 0) {
/* 236 */       return ChannelServer.getInstance(ch).getPlayerStorage().getCharacterById(id);
     }
/* 238 */     return null;
   }

   private static class SingletonHolder
   {
/* 243 */     protected static final WorldFindService instance = new WorldFindService();
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\world\WorldFindService.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */